/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim </a>, 2004
 */
package hk.com.carfield.core;

import hk.com.carfield.content.Content;
import hk.com.carfield.content.Describable;
import hk.com.carfield.persistance.GenericDA;
import hk.com.carfield.util.FileUtil;
import hk.com.carfield.util.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;

public final class PersistenceManager {
	public final GenericDA genericDA;

	private final Environment jbdbEnv;

	private final EntityStore store;

	public PersistenceManager(final Config config) {
		try {
			final EnvironmentConfig envConfig = new EnvironmentConfig();
			envConfig.setAllowCreate(true);
			jbdbEnv = new Environment(config.cachePath, envConfig);
			final StoreConfig storeConfig = new StoreConfig();
			storeConfig.setAllowCreate(true);
			store = new EntityStore(jbdbEnv, "EntityStore", storeConfig);

			genericDA = new GenericDA(store);
		} catch (final DatabaseException dbe) {
			throw new RuntimeException(dbe);
		}

	}

	private File descFile(final File file) {
		final File desc = new File(FileUtil.metaFolder(file), "desc.txt");
		return desc;
	}

	private String get(final File file, final boolean html) throws IOException {
		final File desc = descFile(file);
		if (FileUtil.valid(desc)) {
			final InputStreamReader is = new InputStreamReader(
					new FileInputStream(desc), "UTF-8");
			final String str = html ? StringUtil.text2Html(is) : IOUtils
					.toString(is);
			is.close();
			return str;
		} else {
			return "";
		}

	}

	public String getFullDesc(final Context context, final File file)
			throws IOException {
		final String str = getWithHtml(file);
		if (StringUtil.notBlank(str)) {
			return str;
		} else {
			final Content item = ContentManager.getContent(context, file);
			if (item instanceof Describable) {
				return ((Describable) item).getDesc();
			} else {
				return "";
			}
		}
	}

	public String getPlain(final File file) throws IOException {
		return get(file, false);
	}

	public String getWithHtml(final File file) throws IOException {
		return get(file, true);
	}

	public void persist(final File file, final String value)
			throws FileNotFoundException, IOException {
		if (StringUtil.notBlank(value)) {
			final File desc = descFile(file);
			if (!desc.exists()) {
				desc.getParentFile().mkdirs();
			}
			final OutputStream os = new FileOutputStream(desc);
			IOUtils.write(value, os, "UTF-8");
			os.close();
		}
	}

	public void shutdown() {
		try {
			if (store != null) {
				store.close();
			}
			if (jbdbEnv != null) {
				jbdbEnv.close();
			}
		} catch (final DatabaseException dbe) {
			throw new RuntimeException(dbe);
		}
	}
}